<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
//权限管理
class RoleController extends CommonController {
		public function index(){		
			$this -> assign('title','管理员列表');		
			$mod = M('admin_user');
			$admin_user = $mod -> where('1=1')->select();			
			$this -> assign('admin_user',$admin_user);
			$this -> display('');
		}
		//管理员添加页面
		public function add(){
			$mod = M('admin_role');
			$data = $mod -> field('role') ->select();
			$this -> assign('data',$data);
			$this -> display('');
		}
		//插入管理员
		public function insert(){
			$mod = M('admin_user');
			$mod -> startTrans();
			$post = I('post.');
			$post['last_ip'] = $_SERVER['REMOTE_ADDR'];
			$post['add_time'] = time();
			//处理密码
			$post['ec_salt'] = rand('1111','9999');
			$post['password'] = md5(md5(I('post.password')).$post['ec_salt']);
			unset($post['repassword']);
			$res = $mod -> data($post) -> add();
			//封装日志
					$admin_log = M('admin_log');
					$ad_log['user_name'] = $_SESSION['admin']['user_name'];
					$ad_log['log_time'] = time();
					$ad_log['user_id'] = $_SESSION['admin']['user_id'];
					$ad_log['log_info'] = '管理员添加';
					$ad_log['ip_address']= get_client_ip();
				$res2 = $admin_log -> add($ad_log);
			if($res>0&&$res2>0){
				$mod -> commit();
				$this -> success('添加成功',U('admin/role/index'));
			}else{
				$mod -> rollback();
				$this -> error('添加失败',U('admin/role/index'));
			}
		}
		//修改信息
		public function edit(){
			$this -> assign('title','管理员信息修改');
			$admin = M('admin_user') -> where('user_id = "%d" ',I('get.id'))->find();
			
			$this -> assign('admin',$admin);
			$this -> display('');
		}
		public function update(){
			$mod = M('admin_user');
			$mod -> startTrans();
			$post = I('post.');	
			$post['ec_salt'] = rand('1111','9999');			
			//处理密码			
			$post['password'] = md5(md5(I('post.password')).$post['ec_salt']);
			
			unset($post['repassword']);		
			$res = $mod -> where('user_id = '.I('get.id')) ->save($post);
			//封装日志
				$admin_log = M('admin_log');
				$ad_log['user_name'] = $_SESSION['admin']['user_name'];
				$ad_log['log_time'] = time();
				$ad_log['user_id'] = $_SESSION['admin']['user_id'];
				$ad_log['log_info'] = '公告删除操作';
				$ad_log['ip_address']= get_client_ip();
				$res2 = $admin_log -> add($ad_log);
			if($res >0 && $res2>0){
				$this -> success('修改成功',U('admin/role/index'));
			}else{
				$this -> error('修改失败',U('admin/role/index'));
			}
		}

		public function delete(){
			$mod = M('admin_user');
			$mod -> commit();
			$res = $mod -> where('user_id = "%d" ',I('get.id'))->delete();
			$admin_log = M('admin_log');
			//封装日志
				$ad_log['user_name'] = $_SESSION['admin']['user_name'];
				$ad_log['log_time'] = time();
				$ad_log['user_id'] = $_SESSION['admin']['user_id'];
				$ad_log['log_info'] = '公告删除操作';
				$ad_log['ip_address']= get_client_ip();
				$res2 = $admin_log -> add($ad_log);
			if($res>0 && $res2>0){
				$mod -> commit();
				$this -> success('删除成功',U('admin/role/index'));
			}else{
				$mod -> rollback();
				$this -> error('删除失败',U('admin/role/index'));
			}
		}

		public function admin_log(){		
			$this -> assign('title','管理员日志');
			$mod = M('admin_log');
			
			if(!empty(I('get.admin_name'))){
				$data['user_name'] = I('get.admin_name');
				//设置分页的配置
			 			$page = new Page($mod -> where($data)->where('user_name <> "bb"')->count(),16);			 			
			 			$page->setConfig('prev','上一页');
			 			$page->setConfig('next','下一页');		 		
						
				$logdata = $mod -> where($data)->where('user_name <> "bb"') -> order('log_time desc')->limit($page->firstRow,$page->listRows)->select();
				$url = $page->show();	
				$this -> assign('url',$url);
			}else{
				$page = new Page($mod->where('user_name <> "bb"') ->count(),16);			 			
			 			$page->setConfig('prev','上一页');
			 			$page->setConfig('next','下一页');		
				$logdata = $mod ->where('user_name <> "bb"')-> order('log_time desc')->limit($page->firstRow,$page->listRows)->select();
				$url = $page->show();	
				$this -> assign('url',$url);
			}
			//分页链接
			
			
			$this -> assign('logdata',$logdata);
			$this -> display('');
		}

		//查看管理员操作日志详情

	 public function look(){		
			$this -> assign('title','管理员日志');
			$mod = M('admin_log');
				if(!empty(I('get.log_time'))){
					//限制天数
					$time = time() - I('get.log_time')*24*60*60;
					$data['log_time'] = array('gt',$time);
				}
				$data['user_id'] = array('eq',I('get.id'));
				//设置分页的配置
			 			$page = new Page($mod -> where($data)->count(),6);			 			
			 			$page->setConfig('prev','上一页');
			 			$page->setConfig('next','下一页');		 		
						
				$logdata = $mod -> where($data) -> order('log_time desc')->limit($page->firstRow,$page->listRows)->select();
			
			//分页链接
			$url = $page->show();
			$this -> assign('id',I('get.id'));	
			$this -> assign('url',$url);
			$this -> assign('logdata',$logdata);
			$this -> display('');
		}

		public function index_role(){
			$this -> assign('title','角色列表');
			$mod = M('admin_role');
			$role = $mod -> select();
			$this -> assign('role',$role);
			$this -> display('');
		}
		//分配角色权限
		public function add_role(){
			$this -> assign('title','添加角色');
			$mod_node = M('admin_node');		
			$admin_node_article = $mod_node -> where('role_controller_name = "%s"','article')-> select();
			$admin_node_caiwu = $mod_node -> where('role_controller_name = "%s"','caiwu')-> select();
			$admin_node_check = $mod_node -> where('role_controller_name = "%s"','check')-> select();
			$admin_node_first = $mod_node -> where('role_controller_name = "%s"','first')-> select();
			$admin_node_index = $mod_node -> where('role_controller_name = "%s"','index')-> select();
			$admin_node_system = $mod_node -> where('role_controller_name = "%s"','system')-> select();
			$admin_node_user = $mod_node -> where('role_controller_name = "%s"','user')-> select();
			$admin_node_role = $mod_node -> where('role_controller_name = "%s"','role')-> select();
			
			$this -> assign('admin_node_article',$admin_node_article);
			$this -> assign('admin_node_caiwu',$admin_node_caiwu);
			$this -> assign('admin_node_check',$admin_node_check);
			$this -> assign('admin_node_first',$admin_node_first);
			$this -> assign('admin_node_index',$admin_node_index);
			$this -> assign('admin_node_system',$admin_node_system);
			$this -> assign('admin_node_user',$admin_node_user);
			$this -> assign('admin_node_role',$admin_node_role);
			$this -> display('');
		}
		//添加权限	
		public function insert_role(){
			$action = implode(',',I('post.action'));
		
			$role['role_desc'] = I('post.role_desc');
			$role['role'] = I('post.role_name');
			$role['node_id'] = $action;			
			$mod = M('admin_role');	
			$mod -> startTrans();		
			 $res = $mod-> data($role) -> add();	
			 //封装日志
			 $admin_log = M('admin_log');
			$ad_log['user_name'] = $_SESSION['admin']['user_name'];
			$ad_log['log_time'] = time();
			$ad_log['user_id'] = $_SESSION['admin']['user_id'];
			$ad_log['log_info'] = '权限添加操作';
			$ad_log['ip_address']= get_client_ip();
			$res2 = $admin_log -> add($ad_log);		
			if($res>0 && $res2){
				$mod -> commit();
				$this -> success('添加角色成功',U('admin/role/index'));
			}else{
				$mod -> rollback();
				$this -> error('添加角色失败',U('admin/role/index'));
			}
		}
		//修改角色的权限
		public function edit_role(){
			$mod_role = M('admin_role');			
			$node = $mod_role -> where('id = "%d" ',I('get.id'))->find();
			$role_action = explode(',',$node['node_id']); 
			$this -> assign('role_node',$role_action);			
			$this -> assign('node',$node);			
			//所有的方法
			$mod = M('admin_node');
			$admin_node_article = $mod -> where('role_controller_name = "%s"','article')-> select();
			$admin_node_caiwu = $mod -> where('role_controller_name = "%s"','caiwu')-> select();
			$admin_node_check = $mod -> where('role_controller_name = "%s"','check')-> select();
			$admin_node_first = $mod -> where('role_controller_name = "%s"','first')-> select();
			$admin_node_index = $mod -> where('role_controller_name = "%s"','index')-> select();
			$admin_node_system = $mod -> where('role_controller_name = "%s"','system')-> select();
			$admin_node_user = $mod -> where('role_controller_name = "%s"','user')-> select();
			$admin_node_role = $mod -> where('role_controller_name = "%s"','role')-> select();
			
			$this -> assign('admin_node_article',$admin_node_article);
			$this -> assign('admin_node_caiwu',$admin_node_caiwu);
			$this -> assign('admin_node_check',$admin_node_check);
			$this -> assign('admin_node_first',$admin_node_first);
			$this -> assign('admin_node_index',$admin_node_index);
			$this -> assign('admin_node_system',$admin_node_system);
			$this -> assign('admin_node_user',$admin_node_user);
			$this -> assign('role_action',$role_action);
			$this -> display('');
		}

		//修改权限
		public function  update_role(){
			$mod = M('admin_role');
			$mod -> startTrans();
			$role['node_id'] = implode(',',I('post.action'));
			$role['role'] = I('post.role_name');
			$role['role_desc'] = I('post.role_desc');
			$res = $mod -> where('id = "%d" ',I('get.id'))->save($role);
			$admin_log = M('admin_log');
			$ad_log['user_name'] = $_SESSION['admin']['user_name'];
			$ad_log['log_time'] = time();
			$ad_log['user_id'] = $_SESSION['admin']['user_id'];
			$ad_log['log_info'] = '权限修改操作';
			$ad_log['ip_address']= get_client_ip();
			$res2 = $admin_log -> add($ad_log);
			if($res>0 && $res2>0){
				$mod -> commit();
				$this -> success('更改成功',U('admin/role/index_role'));
			}else{
				$mod -> rollback();
				$this -> error('更改失败',U('admin/role/index_role'));
			}
		}

	
}